//
//  ClubSettingVC.m
//  SelfDrivingTravelling-IOS
//
//  Created by Teplot_01 on 16/8/18.
//  Copyright © 2016年 Teplot_01. All rights reserved.
//

#import "ClubSettingVC.h"
#import "ClubJoinConditionVC.h"
#import "LeaderModifyPasswordVC.h"
#import "VersionInformationVC.h"
#import "AboutVC.h"
#import "LoginVC.h"
@interface ClubSettingVC ()<UITableViewDelegate,UITableViewDataSource>
{
    UITableView *_tableView;
}
@end

@implementation ClubSettingVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = LighterGrayColor;
    self.title = @"设置";
    [self initTableView];
    //去除多余的分割线
    _tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
}
- (void)initTableView{
    _tableView = [[UITableView alloc]initWithFrame:self.view.frame];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [self.view addSubview:_tableView];
}
#pragma - mark UITableViewDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 4;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section == 2) {
        return 2;
    }else{
        return 1;
    }
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [[UITableViewCell alloc]init];
    NSArray *titleArr = [[NSArray alloc] initWithObjects:@"加入条件",@"修改密码",@"版本信息",@"关于我们",nil];
    NSArray *iconArr = [[NSArray alloc] initWithObjects:@"requirement",@"password",@"update",@"about_us",nil];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.textLabel.font = [UIFont systemFontOfSize:15];
    if (indexPath.section == 0) {
        cell.textLabel.text = [titleArr objectAtIndex:indexPath.row];
        cell.imageView.image = [UIImage imageNamed:[iconArr objectAtIndex:indexPath.row]];
    }
    if (indexPath.section == 1) {
        cell.textLabel.text = [titleArr objectAtIndex:indexPath.row+1];
        cell.imageView.image = [UIImage imageNamed:[iconArr objectAtIndex:indexPath.row+1]];
//        if (indexPath.row == 0) {
//            cell.accessoryType = UITableViewCellAccessoryNone;
//            UISwitch *sw = [[UISwitch alloc]initWithFrame:CGRectMake(KScreenWidth - 60, 5, 30, 35)];
//            sw.onTintColor = Theme_Color;
//            [cell.contentView addSubview:sw];
//        }
    }
    if (indexPath.section == 2) {
        cell.textLabel.text = [titleArr objectAtIndex:indexPath.row+2];
        cell.imageView.image = [UIImage imageNamed:[iconArr objectAtIndex:indexPath.row+2]];
    }
    if (indexPath.section == 3) {
        cell.accessoryType = UITableViewCellAccessoryNone;
        cell.textLabel.text = @"退出登陆";
        cell.textLabel.font = [UIFont systemFontOfSize:16];
        cell.textLabel.textColor = Red_Color;
        cell.textLabel.textAlignment = NSTextAlignmentCenter;
    }
    
    return cell;
    
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    NSInteger index = [tableView indexWithIndexPath:indexPath];
    switch (index) {
        case 0:
        {
            ClubJoinConditionVC *vc = [[ClubJoinConditionVC alloc]init];
            vc.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:vc animated:YES];
        }
            break;
        case 1:
        {
            LeaderModifyPasswordVC *vc = [[LeaderModifyPasswordVC alloc]init];
            vc.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:vc animated:YES];
        }
            break;
        case 2:
        {
            VersionInformationVC *vc = [[VersionInformationVC alloc]init];
            vc.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:vc animated:YES];
        }
            break;
        case 3:
        {
            AboutVC *vc = [[AboutVC alloc]init];
            vc.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:vc animated:YES];
        }
            break;
        case 4:
        {
            UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"确定要退出登陆么？" message:nil preferredStyle:UIAlertControllerStyleAlert];
            UIAlertAction *deleteAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action){
                //退出操作
                //清除缓存
                [@"" saveToNSDefaultsWithKey:kPhotoUrl];
                [@"" saveToNSDefaultsWithKey:kClubPhotoUrl];
                [@"" saveToNSDefaultsWithKey:kRealName];
                [@"" saveToNSDefaultsWithKey:kNickName];
                [@"" saveToNSDefaultsWithKey:kSex];
                [@"" saveToNSDefaultsWithKey:kCity];
                [@"" saveToNSDefaultsWithKey:kEmail];
                [@"" saveToNSDefaultsWithKey:kPhone];
                [@"" saveToNSDefaultsWithKey:kPass];
                [@"" saveToNSDefaultsWithKey:kAge];
                [@"" saveToNSDefaultsWithKey:kIsLogined];
                [@"" saveToNSDefaultsWithKey:kType];
                
                //club
                [@"" saveToNSDefaultsWithKey:kIdCardFimgUrl];
                [@"" saveToNSDefaultsWithKey:kIdCardBimgUrl];
                [@"" saveToNSDefaultsWithKey:kStatus];
                [@"" saveToNSDefaultsWithKey:kClubName];
                [@"" saveToNSDefaultsWithKey:kClubSummary];

                LoginVC *vc = [[LoginVC alloc]init];
                [self presentViewController:vc animated:NO completion:nil];
//                self.view.window.rootViewController = vc;
            }];
            UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
            [alertController addAction:deleteAction];
            [alertController addAction:cancelAction];
            [self presentViewController:alertController animated:YES completion:nil];
        }
            break;
//
    }
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 40;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 15;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
